
package protocol.transactions;

import preferMgr.XonePrefHelper;
import protocol.XoneProtocol;
import protocol.XoneService;
import protocol.XoneServiceCode;
import protocol.meta.APPSetting;
import android.content.Context;
import app.BaseApplication;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.netease.http.THttpRequest;
import com.netease.pkgRelated.ErrorToString;

public class GetAppSettingTransaction extends XoneBaseTransaction {

    public GetAppSettingTransaction() {
        super(TRANSACTION_TYPE_GET_APP_SETTING);
    }

    @Override
    protected void onXoneTransactionSuccess(int code, Object obj) {
        if (obj != null && obj instanceof JsonElement) {
            JsonElement json = ((JsonElement)obj).getAsJsonObject().get("config");
            if (json != null) {
                Gson gson = new Gson();
                APPSetting setting = gson.fromJson(json, APPSetting.class);
                if (setting != null) {
                    Context context = BaseApplication.getAppInstance();
//                    NTLog.e("GetAppSettingTransaction","GetApp Success01 setting.isBanPush() is " + setting.isBanPush());
                    if (setting.getTimestamp() > XonePrefHelper.getSettingUpdateTime(context)) {
                        XonePrefHelper.putSettingUpdateTime(context, setting.getTimestamp());
                        XonePrefHelper.putAtMeOn(context, setting.isAtMe());
                        XonePrefHelper.putNewCommentOn(context, setting.isNewComment());
                        XonePrefHelper.putNewFansOn(context, setting.isNewFans());
                        XonePrefHelper.putNewFriendDynamicOn(context, setting.isFriendDynamic());
                        XonePrefHelper.putNewMessageOn(context, setting.isNewLetter());
                        XonePrefHelper.putPushOn(context, !setting.isBanPush());
                        XonePrefHelper.putReceiveAllComment(context, setting.isReceiveAllComment());
                        XonePrefHelper.putReceiveAllMessage(context, setting.isReceiveAllMessage());
                        XonePrefHelper.putShockOn(context, setting.isShock());
                        XonePrefHelper.putSoundOn(context, setting.isSound());
//                        NTLog.e("GetAppSettingTransaction","GetApp Success02 setting.isBanPush() is " + setting.isBanPush());
                    } else if (setting.getTimestamp() < XonePrefHelper
                            .getSettingUpdateTime(context)) {// 上传本地设置
                        XoneService.getInstance().doSetAppSetting();
                    }
                    notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS, setting);
                    return;
                }
            }
        }

        notifyError(XoneServiceCode.ERR_CODE_DATA_PARSE_EXCEPTION,
                ErrorToString.getString(XoneServiceCode.ERR_CODE_DATA_PARSE_EXCEPTION));

    }

    @Override
    public void onTransact() {
        THttpRequest request = XoneProtocol.getInstance().createGetAppSettingRequest();
        sendRequest(request);
    }

}
